
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class EditUser extends CI_Controller {

    function __construct() {
        parent::__construct(); // contructor del controlador xD
    }

    /**
     * carga vistas de cuenta
     *
     */
    function index() {
        $this->load->helper(array('form', 'url'));
        $logged_in = $this->session->userdata('logged_in');
        $data['logged_in'] = $logged_in;
        $data['id'] = $logged_in['id'];
        $data['email'] = $logged_in['email'];
        $data['username'] = $logged_in['username'];
        $data['request'] = false;
        $this->load->view('perfil/header_view');
        $this->load->view('perfil/menucount_view');
        $this->load->view('perfil/configcount_view', $data);
    }

    /**
     * controlador para gestionar cuenta y editar password
     *
     */
    function account() {

        $logged_in = $this->session->userdata('logged_in');
        $data['request'] = false;
        $data['logged_in'] = $logged_in;
        $data['id'] = $logged_in['id'];
        $data['email'] = $logged_in['email'];
        $data['username'] = $logged_in['username'];
        //$this->
        $password = $this->input->post('password');
        $newpassword = $this->input->post('newpassword');


        if ($password && $newpassword) {

            // verificar login y luego cambiar la password
            $this->load->model('login/user_model', '', TRUE);
            $requestPass = $this->user_model->verifyPassword($data['id'], $password);

            if ($requestPass) {
                $this->user_model->editPassword($data['id'], $password, $newpassword);
                $data['request'] = "pass cambiada con exito";
            } else {
                $data['request'] = "error al cambiar pass";
            }

            //cargar vista 
            $this->load->view('perfil/header_view');
            $this->load->view('perfil/menucount_view');
            $this->load->view('perfil/configcount_view', $data);
        } else {

            //cargar vista
            $this->load->view('perfil/header_view');
            $this->load->view('perfil/menucount_view');
            $this->load->view('perfil/configcount_view', $data);
        }
    }

    /**
     * carga vista persona
     */
    function personal() {

        $option = $this->input->post('option');

        switch ($option) {

            case 'foto':

                $logged_in = $this->session->userdata('logged_in');
                $data['logged_in'] = $logged_in;
                $linkfoto = $this->input->post('fotoperfil');
                $this->load->model('perfil/perfil_model', '', TRUE);

                //cargar datos
                $data['foto'] = $this->perfil_model->getImage($logged_in['id']);
                $data['names'] = $this->perfil_model->getNames($logged_in['id']);
                $data['lastname'] = $this->perfil_model->getLastname($logged_in['id']);
                $data['presentation'] = $this->perfil_model->getPresentation($logged_in['id']);

                //configuración para la imagen que se va a subir 
                $config['upload_path'] = './users/';
                //$config['filename'] = 'userfile';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '1000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';
                $config['file_name'] = $logged_in['id'];
                $config['overwrite'] = true;



                //cargar biblioteca upload y pasar parametros para validar
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {

                    $file = $this->upload->data();
                    $this->perfil_model->setImage($logged_in['id'], $file['file_name']);
                    $data['foto'] = $this->perfil_model->getImage($logged_in['id']);
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                } else {

                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    //$data['error']='hola';
                    //$data['error']=var_dump($this->upload->data());
                    $this->load->view('perfil/configpersonal_view', $data);
                }

                break;

            case 'nombres':

                $logged_in = $this->session->userdata('logged_in');
                $data['logged_in'] = $logged_in;
                $this->load->model('perfil/perfil_model', '', TRUE);
                //cargar datos
                $data['foto'] = $this->perfil_model->getImage($logged_in['id']);
                $data['names'] = $this->perfil_model->getNames($logged_in['id']);
                $data['lastname'] = $this->perfil_model->getLastname($logged_in['id']);
                $data['presentation'] = $this->perfil_model->getPresentation($logged_in['id']);

                $names = $this->input->post('nombres');


                if ($names) {
                    $this->perfil_model->setName($logged_in['id'], $names);
                    $data['names'] = $this->perfil_model->getNames($logged_in['id']);
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                } else {
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                }

                break;

            case 'apellidos':

                $logged_in = $this->session->userdata('logged_in');
                $data['logged_in'] = $logged_in;
                $this->load->model('perfil/perfil_model', '', TRUE);
                //cargar datos
                $data['foto'] = $this->perfil_model->getImage($logged_in['id']);
                $data['names'] = $this->perfil_model->getNames($logged_in['id']);
                $data['lastname'] = $this->perfil_model->getLastname($logged_in['id']);
                $data['presentation'] = $this->perfil_model->getPresentation($logged_in['id']);

                $lastname = $this->input->post('apellidos');

                if ($lastname) {
                    $this->perfil_model->setLastname($logged_in['id'], $lastname);
                    $data['lastname'] = $this->perfil_model->getLastname($logged_in['id']);
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                } else {
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                }

                break;

            case 'presentacion':

                $logged_in = $this->session->userdata('logged_in');
                $data['logged_in'] = $logged_in;
                $this->load->model('perfil/perfil_model', '', TRUE);
                //cargar datos
                $data['foto'] = $this->perfil_model->getImage($logged_in['id']);
                $data['names'] = $this->perfil_model->getNames($logged_in['id']);
                $data['lastname'] = $this->perfil_model->getLastname($logged_in['id']);
                $data['presentation'] = $this->perfil_model->getPresentation($logged_in['id']);

                $presentation = $this->input->post('presentacion');

                if ($presentation) {
                    $this->perfil_model->setPresentation($logged_in['id'], $presentation);
                    $data['presentation'] = $this->perfil_model->getPresentation($logged_in['id']);
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                } else {
                    $this->load->view('perfil/header_view');
                    $this->load->view('perfil/menucount_view');
                    $this->load->view('perfil/configpersonal_view', $data);
                }
                break;

            default:

                $logged_in = $this->session->userdata('logged_in');
                $data['logged_in'] = $logged_in;
                $this->load->model('perfil/perfil_model', '', TRUE);
                //cargar datos
                $data['foto'] = $this->perfil_model->getImage($logged_in['id']);
                $data['names'] = $this->perfil_model->getNames($logged_in['id']);
                $data['lastname'] = $this->perfil_model->getLastname($logged_in['id']);
                $data['presentation'] = $this->perfil_model->getPresentation($logged_in['id']);

                $this->load->view('perfil/header_view');
                $this->load->view('perfil/menucount_view');
                $this->load->view('perfil/configpersonal_view', $data);

                break;
        }
    }

    /**
     *  carga vista intereses
     *
     */
    function interests() {

        $this->load->view('perfil/header_view');
        $this->load->view('perfil/menucount_view');
        $this->load->view('perfil/configinterests_view');
    }

    /**
     * Ejemplo de carga de intereses de un usuario
     * @param type $usr_id 
     */
    function ejemploIntereses($usr_id) {
        $this->load->model('perfil/perfil_model');
        $this->load->library('interes');
        $data = array();
        $data = $this->perfil_model->getInterest($usr_id);
        $interes = new interes();
        for ($i = 0; $i < count($data); $i++) {
            $interes = $data[$i];
            echo $interes->getInt_id();
            echo "<br>";
            echo $interes->getInt_nombre();
            echo "<br>";
            echo $interes->getInt_descripcion();
            echo "<br>";
            echo $interes->getIntp_nivel_interes();
            echo "<br>";
            echo "<br>";
        }
    }

}

?>